package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;

import net.oschina.app.adapter.BaseNewsInfosAdapter;
import net.oschina.app.base.BaseAdapter;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.bean.GeneralNewsBean;
import net.oschina.app.bean.GeneralNewsDetailBean;
import net.oschina.app.protocol.DetailNewsInfosProtocol;
import net.oschina.app.protocol.NewsInfosProtocol;
import net.oschina.app.ui.DetailActivity;
import net.oschina.app.util.CommonUtils;

import java.util.List;

/**
 * Created by wei03 on 2017/1/14.
 */
public class NewsInfosFragment extends BaseFragment {

    private BaseNewsInfosAdapter mBaseNewsInfosAdapter;
    private List<GeneralNewsBean.NewsBean> mNewslist;

    @Override
    protected BaseAdapter getRvAdapter() {
        mBaseNewsInfosAdapter = new BaseNewsInfosAdapter();
        return mBaseNewsInfosAdapter;
    }

    @Override
    protected void OnSubItemClick(RecyclerAdapterWithHF adapter, RecyclerView.ViewHolder vh, int position) {
        super.OnSubItemClick(adapter, vh, position);
        final Intent intent = new Intent(getContext(), DetailActivity.class);
        GeneralNewsBean.NewsBean newsBean = mNewslist.get(position);
        final int id = newsBean.getId();
        new Thread(new Runnable() {
            @Override
            public void run() {
                DetailNewsInfosProtocol detailNewsInfosProtocol = new DetailNewsInfosProtocol(id);
                GeneralNewsDetailBean generalNewsDetailBean = detailNewsInfosProtocol.getData(0);
                intent.putExtra("url", generalNewsDetailBean.getNews().getUrl());
                intent.putExtra("title", "资讯详情");
                startActivity(intent);
            }
        }).start();
    }
    @Override
    protected int onLoadSubData() {
        Bundle arguments = getArguments();
        String type = "";           //默认加载资讯Fragment
        if (arguments != null) {
            type = arguments.getString("type");//不为空,加载热点Fragment
        }
        NewsInfosProtocol newsInfosProtocol = new NewsInfosProtocol(type);
        newsInfosProtocol.setPageIndex(mPageIndex);
        final GeneralNewsBean generalNewsBean = newsInfosProtocol.getData(mList.size() / 20);
        if (generalNewsBean != null) {
            mPageIndex++;
            mNewslist = generalNewsBean.getNewslist();
            if (mNewslist.size() == 0 && mPageIndex == 0) {            //没有数据
                return STATE_EMPTY;
            }
            mList.addAll(mNewslist);
            CommonUtils.runOnMainThread(new Runnable() {
                @Override
                public void run() {
                    if (mNewslist.size() == 0) {
                        Toast.makeText(getContext(), "没有更多数据", Toast.LENGTH_SHORT).show();
                        ptrClassicFrameLayout.setLoadMoreEnable(false);
                    }
                    mBaseNewsInfosAdapter.notifyDataSetChanged();
                }
            });
            return STATE_SUCCESS;
        }
        return STATE_EMPTY;
    }
}
